package com.lou.springboot.config;

import com.lou.springboot.interceptor.AdminLoginInterceptor;
import com.lou.springboot.interceptor.ReaderInterceptor;
import com.lou.springboot.interceptor.TrackInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class MyBlogWebMvcConfigurer implements WebMvcConfigurer {

    @Value("${admin.skipLogin}")
    private Boolean skipLogin;

    @Autowired
    private AdminLoginInterceptor adminLoginInterceptor;

    @Autowired
    private ReaderInterceptor readerInterceptor;

    @Autowired
    private TrackInterceptor trackInterceptor;

    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(readerInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns("/admin/**");
        registry.addInterceptor(trackInterceptor)
                .excludePathPatterns("/blog/css/**")
                .excludePathPatterns("/blog/img/**")
                .excludePathPatterns("/blog/js/**")
                .excludePathPatterns("/admin/**")
                .excludePathPatterns("/common/**");
        if (skipLogin) return;
        registry.addInterceptor(adminLoginInterceptor)
                .addPathPatterns("/admin/**")
                .excludePathPatterns("/admin/login")
                .excludePathPatterns("/admin/dist/**")
                .excludePathPatterns("/admin/plugins/**");
    }

}
